<?php
namespace Pay\Service\Wxpay;

use Pay\Config\Wxpay;

class ActionAbstract
{
    protected $config;
    protected $aopclient;

    public function __construct($config = false) {
        $this->config = (new Wxpay())->config();
        if ($config) {
            $this->config = $config;
        }
    }

    protected function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            $temp = "<".$key."><![CDATA[".$val."]]></".$key.">";
            if (is_numeric($val)) {
                $temp = "<".$key.">".$val."</".$key.">";
            }
            $xml .= $temp;
        }
        $xml.="</xml>";
        return $xml;
    }

    protected function xmlToArray($xml)
    {
        if (!$xml) {
            return false;
        }
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $data;
    }
    /*
     * $params严格为以下参数
     * $params['timeStamp']时间戳
     * $params['nonceStr']随机数
     * $params['package']包
     * $params['signType']加密类型
     */
    protected function sign($params)
    {
        $param['appId'] = $this->config['appId'];
        $param['timeStamp'] = $params['timeStamp'];
        $param['nonceStr'] = $params['nonceStr'];
        $param['package'] = $params['package'];
        $param['signType'] = $params['signType'];
        ksort($param);
        $str = '';
        foreach ($param as $key => $value) {
            $str .= $key. '=' . $value . '&';
        }
        $signStr = $str . 'key=' . $this->config['key'];
        $sign = strtoupper(md5($signStr));
        return $sign;
    }
}
?>
